//
//  NovelFristTableViewCell.m
//  A_Program
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "NovelFristCollectionViewCell.h"
#import "NovelFristModel.h"
#import  <SDWebImage/UIImageView+WebCache.h>
#import "NovelFristImageView.h"
@interface NovelFristCollectionViewCell ()<UIScrollViewDelegate>
@end
@implementation NovelFristCollectionViewCell
- (void)dealloc
{
    [self.fristMainImageView release];
    [self.fristMainSlider release];
    [self.fristMainScrollView release];
    [self.timer release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.fristMainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/4)];
        [self.contentView addSubview:self.fristMainScrollView];
        //加代理
        self.fristMainScrollView.delegate = self;
        //一张一张转动
        self.fristMainScrollView.pagingEnabled = YES;
        //定义一下cell的大小
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/4);
        [self setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:0.4]];
        [self.fristMainScrollView release];

    }
    return self;
}
- (void)createViewData:(NSArray *)array{
    NSMutableArray *array1 = [NSMutableArray arrayWithArray:array];
    NovelFristModel *firstModel = [array1 firstObject];
    NovelFristModel *lastModel = [array1 lastObject];
    [array1 insertObject:lastModel atIndex:0];
    [array1 addObject:firstModel];
    NSInteger count = 0;
    [self.fristMainScrollView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width * [array1 count], 10)];
    for (NovelFristModel *mainModel in array1) {
        
        self.fristMainImageView = [[[NovelFristImageView alloc]initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width )*count,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height/4)]autorelease];
        
        self.fristMainImageView.novelFristModel = mainModel;
        
        [self.fristMainImageView sd_setImageWithURL:[NSURL URLWithString:mainModel.pic] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        [self.fristMainScrollView addSubview:self.fristMainImageView];
        UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]autorelease];
        [self.fristMainImageView addGestureRecognizer:tap];
        
        count ++;
    }
    // 设置时间部分
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    //定义时间并调用timerAction方法
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
}
- (void)timerAction:(NSTimer *)timer
{
    self.page++;
    CGFloat offWidth = self.fristMainScrollView.frame.size.width * self.page;
    NSInteger num = self.fristMainScrollView.contentSize.width / self.fristMainScrollView.frame.size.width;
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.fristMainScrollView setContentOffset:CGPointMake(offWidth, 0)];
    }];
    if (self.page == num - 1) {
        self.page = 1;
        [self.fristMainScrollView setContentOffset:CGPointMake(self.fristMainScrollView.frame.size.width, 0)];
    }
}
//用于图片滚动的偏移计算
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint offPoint = scrollView.contentOffset;
    CGSize size = scrollView.contentSize;
    CGFloat width = scrollView.frame.size.width;
    
    self.page = offPoint.x / width;
    
    if (offPoint.x == size.width - width) {
        self.page = 1;
        [scrollView setContentOffset:CGPointMake(width, 0)];
    }
    if (offPoint.x == 0) {
        self.page = scrollView.contentSize.width / scrollView.frame.size.width - 2;
        
        [scrollView setContentOffset:CGPointMake(size.width - width * 2, 0)];
    }
    
}


//图片手势触发方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
    NovelFristImageView *novelFristImageView = (NovelFristImageView *)tap.view;
    [self.novelCollectionDelegate createNovelFristImageView:novelFristImageView];
}

- (void)awakeFromNib {
    // Initialization code
}



@end
